package T6_commite;
public class T6_commite_notifyAll extends Thread {    
    public static void main(String[] args) {    
    	 Calculator calculator = new Calculator();    
         new ReaderResult(calculator).start();    
         new ReaderResult(calculator).start();    
         new ReaderResult(calculator).start();    
         calculator.start(); 
    }    
} 
class ReaderResult extends Thread {    
    Calculator c;    
    public ReaderResult(Calculator c) {    
    	this.c = c;    
    }    
    public void run() {    
    	synchronized (c) {    
    		try {    
    			System.out.println(Thread.currentThread() + "等待计算结果。。。");    
    			if(Calculator.flag){
    				c.wait();    
    			}
    		} catch (InterruptedException e) {    
    			e.printStackTrace();    
    		}    
    		System.out.println(Thread.currentThread() + "计算结果为：" + c.total);    
    	}    
    }      
}  
class Calculator extends Thread {    
    int total;    
    static boolean flag = true;
    public void run() {    
    	synchronized (this) {    
    		for (int i = 0; i < 101; i++) {    
    			total += i;    
    		}    
    	}    
    	notifyAll();    
    	flag = false;
    }    
}
